
"""
Register a connector function for obtaining a connection to a gadfly SQL database
before executing the dispatcher.

(primarily for testing).
"""

import chatLogic
from whiff.middleware import misc
from whiff import resolver
from whiff import whiffenv

class gadflyTestConnection(misc.utility):
    def __init__(self,
                 Dispatcher,
                 Archive,
                 DbPrefix="Testgf",
                 ):
        self.Dispatcher = Dispatcher
        self.Archive = Archive
        self.DbPrefix = DbPrefix
    def __call__(self, env, start_response):
        # materialize parameters
        Archive = self.param_value(self.Archive, env).strip()
        DbPrefix = self.param_value(self.DbPrefix, env).strip()
        # define the connector function
        def getConnection():
            connection = chatLogic.getNucularConnection(Archive, DbPrefix, create=False)
            return connection
        env = chatLogic.registerConnector(getConnection, env)
        # deliver the dispatch page
        return self.deliver_page(self.Dispatcher, env, start_response)

__middleware__ = gadflyTestConnection

